package com.k7.mobile.platform.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.k7.mobile.platform.bo.IBanIpBO;
import com.k7.mobile.platform.bo.IUserBO;
import com.k7.mobile.platform.dataobject.BanIpDO;
import com.k7.mobile.platform.util.Base64;
import com.k7.mobile.platform.util.DateTimeUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.qike.java.api.UserInfo;
import com.qike.java.api.impl.UserInfoImpl;
import com.qike.java.pojo.User;
public abstract class BaseAction extends ActionSupport implements SessionAware{	
	protected IBanIpBO banIpBO;
	protected IUserBO userBO;
	protected Map session;
	protected Logger log = Logger.getLogger(this.getClass()); 
	
	private String domainRoot;	
	private String staticsRoot;
	
	public void setUserBO(IUserBO userBO) {
		this.userBO = userBO;
	}
    
	public IUserBO getUserBO() {
		return userBO;
	}
	public void setBanIpBO(IBanIpBO banIpBO) {
		this.banIpBO = banIpBO;
	}
	
	public IBanIpBO getBanIpBO() {
		return banIpBO;
	}
	public String getDomainRoot() {
		return domainRoot;
	}
	public void setDomainRoot(String domainRoot) {
		this.domainRoot = domainRoot;
	}
	public void setSession(Map session) {
		this.session = session;
	}		
	
	public String getStaticsRoot() {
		return staticsRoot;
	}
	public void setStaticsRoot(String staticsRoot) {
		this.staticsRoot = staticsRoot;
	}
	public void setXmlOutput(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/xml;charset=UTF-8");  
		 clearResponseCache();  
	}
	public void clearResponseCache(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setHeader("Cache-Control", "no-cache"); 
		 response.setHeader("Pragma","No-cache");        
		 response.setDateHeader("Expires",   0);     
	}
	
	
    public String urlEncode(String str,String charset) {
		try {
			str = URLEncoder.encode(str, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
    
    public String urlEncode(String str) {
    	return urlEncode(str, "UTF-8");
	}
    
    public String urlDecode(String str, String charset){
    	try {
			str = URLDecoder.decode(str, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
    }
    
    public String urldecode(String str) {
    	return urlDecode(str, "gbk");
	}
    
    public String formatTime(Date date) {
		return DateTimeUtil.getFormatDateTime(date);
	}
    
    public String formatDate(Date date) {
		return DateTimeUtil.getFormatDate(date);
	}
    
    public String formatCent2Yuan(Long cent) {
    	double number = cent / 100d;
    	return formatDouble(number);
	}
    
    public String formatDouble(Double number) {
    	DecimalFormat df = new DecimalFormat("#.##");     	
    	return df.format(number);
	}
    
    public String shortString(String input, int len) {
    	if(StringUtils.isEmpty(input)){
    		return input;
    	}
    	
    	if(input.length() < len){
    		return input;
    	}
        return StringUtils.substring(input, 0, len) + "...";
    }
        
    public String base64AndUrlEncode(String str){
    	return urlEncode(Base64.encodeString(str));
    }
    
    public String base64AndUrlDecode(String str){
    	return Base64.decodeString(str);
    }
    
    public User getUser(){
    	HttpServletRequest request = ServletActionContext.getRequest();
    	UserInfo ui = new UserInfoImpl();
    	User user = ui.isLogin(request);
    	
    	return user;
    }
    
         
    /**
     * 请求ip是否是黑名单ip
     * @return
     */
    public boolean isBanIp() {
    	//IP过滤
		String ip = ServletActionContext.getRequest().getRemoteAddr();
		if(ip != null){
			BanIpDO queryDO = new BanIpDO();
			queryDO.setIp(ip);
			BanIpDO resultDO = banIpBO.getBanIp(queryDO);
			if(resultDO != null){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
		
    }
    
    public Map<Long ,User> getUsersByUids(List<String> uids) {
		if(uids == null || uids.size() == 0)
			return null;
		
		List<User> list = userBO.getUsers(uids);
		if(list==null||list.isEmpty()){
			return null;	
		}
		Map<Long,User> map = new HashMap<Long,User>();
		for(User user:list){
			if(user != null)
				map.put(user.getUid().longValue(), user);
		}
		//匿名游客初始化
		User visitor = new User();
		visitor.setNick("匿名用户");
		visitor.setUid(0);
		visitor.setIcon_m("http://sface.7k7kimg.cn/default_uicons/photo_default_1_13_xm.jpg");
		map.put(0L, visitor);
		
		return map;
	}
   
}
